package com.wydpp.gb28181.schedule;

import com.wydpp.gb28181.bean.SipPlatform;
import com.wydpp.gb28181.commander.SIPCommander;
import com.wydpp.repository.DeviceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 发送心跳定时任务
 */
@Slf4j
@Component
public class DeviceKeepaliveSchedule {

    @Autowired
    private SipPlatform sipPlatform;

    @Autowired
    private SIPCommander sipCommander;

    @Autowired
    private DeviceRepository deviceRepository;

    @Scheduled(initialDelay = 3, fixedDelay = 1,timeUnit = TimeUnit.SECONDS)
    public void checkSipDeviceStatus() {

        log.info("发送心跳信息=====================");
        // List<Device> devices = deviceRepository.findRegisteredDevice();
        //
        // if (CollUtil.isNotEmpty(devices)){
        //     for (Device device : devices) {
        //         if (device.getOnline() && device.getKeepaliveTime() != null) {
        //             long expires = sipPlatform.getKeepTimeout() * 1000;
        //             long keepaliveTime = device.getKeepaliveTime();
        //             long nowTime = System.currentTimeMillis();
        //             if (nowTime - keepaliveTime >= expires) {
        //                 sipCommander.keepalive(sipPlatform, device,eventResult->{
        //                     device.setKeepaliveTime(nowTime);
        //                 });
        //             }
        //         }
        //     }
        // }

    }
}
